//
//  WWY_AppTools.m
//  Pianke
//
//  Created by Daydream on 15/12/23.
//  Copyright © 2015年 dada. All rights reserved.
//

#import "WWY_AppTools.h"

@implementation WWY_AppTools

+ (CGSize)SizeForCommitSize:(CGSize)size andFont:(UIFont*)font andContent:(NSString *)string{
    NSDictionary *dic = @{NSFontAttributeName:font};
    
    CGSize meme = [string boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil ].size;
    return meme;
}



+(UIImage *)image:(UIImage*)image byScalingToSize:(CGSize)targetSize
{
    UIImage *sourceImage = image;
    UIImage *newImage = nil;
    
    UIGraphicsBeginImageContext(targetSize);
    
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = CGPointZero;
    thumbnailRect.size.width  = targetSize.width;
    thumbnailRect.size.height = targetSize.height;
    
    [sourceImage drawInRect:thumbnailRect];
    
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage ;
}

+(MJRefreshGifHeader *)headerWithRefreshingBlock:(MJRefreshComponentRefreshingBlock)refreshingBlock{
    
    NSMutableArray *refreshArray = [NSMutableArray array];
    for (NSInteger i = 0; i < 28; i++) {
        NSString *name = [NSString stringWithFormat:@"refresh%ld", i];
        NSString * filePath = [[NSBundle mainBundle] pathForResource:name ofType:@"png"];
        
        UIImage *image = [UIImage imageNamed:filePath];
        image = [WWY_AppTools image:image byScalingToSize:CGSizeMake(image.size.width / 3, image.size.height / 3)];
        
        [refreshArray addObject:image];
        
    }

    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:refreshingBlock];
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
    [header setImages:refreshArray forState:MJRefreshStateRefreshing];
    
    return header;
    
}


+(MJRefreshBackGifFooter *)footerWithRefreshingBlock:(MJRefreshComponentRefreshingBlock)refreshingBlock{
    
    NSMutableArray *imageArray = [NSMutableArray array];
    for (NSInteger i = 1; i < 51; i++) {
        NSString *name = [NSString stringWithFormat:@"a18d7359gw1ewdbb5kj49g20qo02fmyr-2－%ld（被拖移）", i];
        NSString * filePath = [[NSBundle mainBundle] pathForResource:name ofType:@"tiff"];
        
        
        UIImage *image = [UIImage imageNamed:filePath];
        
        image = [WWY_AppTools image:image byScalingToSize:CGSizeMake(image.size.width / 2, image.size.height / 1.5)];
        
        [imageArray addObject:image];
        
    }

    MJRefreshBackGifFooter *footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:refreshingBlock];
    
    
    
    
    [footer setImages:imageArray forState:MJRefreshStateRefreshing];
    footer.stateLabel.hidden = YES;
    
    
    return footer;
}

@end
